<%--
  Created by IntelliJ IDEA.
  User: Gao
  Date: 2024/4/15
  Time: 14:17
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <link rel="stylesheet" href="/static/layui/css/layui.css">
    <script src="/static/jquery-2.1.4.js" type="text/javascript" charset="utf-8"></script>
    <script src="/static/layui/layui.js" type="text/javascript" charset="utf-8"></script>
    <script src="/static/mylayer.js" type="text/javascript" charset="utf-8"></script>
    <script src="/static/kindeditor/kindeditor.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<%--搜索form表单--%>
<form class="layui-form layui-row layui-col-space16">
    用户名：
    <div class="layui-inline layui-input-wrap">
        <input type="text" name="name" value="" class="layui-input" lay-affix="clear">
    </div>
    <div class="layui-inline">
        <button class="layui-btn" lay-submit lay-filter="submitSearch">搜索</button>
        <button type="reset" class="layui-btn layui-btn-primary">清空</button>
    </div>
</form>
<table class="layui-hide" id="test" lay-filter="test"></table>
<%--头部工具条--%>
<script id="toolbarDemo" type="text/html">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
    </div>
</script>
<%--右侧工具条--%>
<script type="text/html" id="barDemo">
    <div class="layui-clear-space">
        <a class="layui-btn layui-btn-xs" lay-event="detail">详情</a>
    </div>
</script>
<%--table中展示图片的templet--%>
<script type="text/html" id="photoTemplet">
    <img src="/pic/{{d.photo}}"></img>
</script>
<%--table中展示status的templet--%>
<script type="text/html" id="statusTemplet">
    <input type="checkbox" name="status" value="{{= d.id }}" title="正常|停用" lay-skin="switch"
           lay-filter="statusFilter" {{= d.status == 1 ? "checked" : "" }}>
</script>

<script>
    layui.use(['table', 'form', 'upload'], function () {
        var form = layui.form;
        var table = layui.table;

        // 状态 - 开关操作
        form.on('switch(statusFilter)', function(obj){
            //update blog set status=? where id=1;
            var id = this.value;
            var checked = obj.elem.checked;
            console.log(checked);
            var status = checked ? 1 : 0;
            $.post(
                '/user?method=updateStatus',
                {'id': id, 'status': status},
                function (result) {
                    if (result.code == 0) {
                        mylayer.okMsg(result.msg);
                    }
                },
                'json'
            );
        });

        $.post(
            '/user?method=selectAll',
            function (result) {
                if (result.code == 0) {
                    $(result.data).each(function () {
                        $('#userId').append('<option value="'+this.id+'">'+this.name+'</option>');
                    })
                }
            },
            'json'
        );
        // 创建渲染实例
        table.render({
            elem: '#test',
            id: 'tableId',
            url: '/user?method=selectByPage',
            toolbar: '#toolbarDemo',
            cols: [[
                {type: 'checkbox', fixed: 'left'},
                {field: 'id', title: 'ID', sort: true, width: 100},
                {field: 'name', title: '用户名'},
                {field: 'role', title: '角色'},
                {field: 'nickName', title: '昵称'},
                {field: 'photo', title: '照片', templet: '#photoTemplet', width: 150},
                {field: 'email', title: '邮件'},
                {field: 'phone', title: '电话'},
                {field: 'status', title: '状态',templet: '#statusTemplet', width: 100},
                {field: 'createTime', title: '创建时间', width: 170},
                {field: 'updateTime', title: '更新时间', width: 170},
                {fixed: 'right', title: '操作', width: 134, minWidth: 125, toolbar: '#barDemo'}
            ]],
            page: true
        });

        // 搜索提交
        form.on('submit(submitSearch)', function (data) {
            var field = data.field; // 获得表单字段
            // 执行搜索重载
            table.reload('tableId', {
                page: {
                    curr: 1 // 重新从第 1 页开始
                },
                where: field // 搜索的字段
                //http://localhost:8080/user?method=selectByPage&page=1&limit=10&name=zhansgsan&address=5&time=2014
            });
            return false; // 阻止默认 form 跳转
        });

        // 头部工具栏事件
        table.on('toolbar(test)', function (obj) {
            var id = obj.config.id;
            var checkStatus = table.checkStatus(id);
            var othis = lay(this);
            switch (obj.event) {
                case 'add':
                    var index = layer.open({
                        type: 2,
                        title: '添加数据',
                        area: ['90%', '90%'], // 宽高
                        content: '/page/user/add'
                    });
                    break;
                case 'LAYTABLE_TIPS':
                    layer.alert('自定义工具栏图标按钮');
                    break;
            }
            ;
        });

        // 触发单元格工具事件
        table.on('tool(test)', function (obj) { // 双击 toolDouble
            var data = obj.data; // 获得当前行数据
            console.log(obj);
            //{"id": 1,"name": "Java1807","address": "dfdf","time": "3434"}
            console.log(obj.data);
            if (obj.event === 'update') {
                var index = layer.open({
                    type: 2,
                    title: '编辑数据',
                    area: ['90%', '90%'], // 宽高
                    content: '/page/user/update?id='+data.id,
                });
            } else if (obj.event === 'detail') {
                layer.open({
                    type: 1,
                    title: data.name,
                    area: ['90%', '90%'], // 宽高
                    content: data.photos,
                })
            }
        });

    });
</script>
</body>
</html>